﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ActivePalInterop.Data;
using System.Collections.ObjectModel;
using ActivePalInterop;
using System.ComponentModel;
using System.Collections;

namespace ActivePalControls
{
    /// <summary>
    /// Interaction logic for PeopleView.xaml
    /// </summary>
    public partial class PeopleView : UserControl
    {
        private ObservableCollection<Person> peopleItemSource;
        private GridViewColumnHeader lastHeaderClicked = null;
        private ListSortDirection lastDirection = ListSortDirection.Ascending;

        #region Properties

        /// <summary>
        /// Gets or sets item source that is bound to the control
        /// </summary>
        public ObservableCollection<Person> PeopleItemSource
        {
            get { return peopleItemSource; }
            set { peopleItemSource = value; }
        }

        /// <summary>
        /// Gets or sets currently selected items
        /// </summary>
        public IList SelectedItems { get; set; }

        #endregion

        /// <summary>
        /// Creates new People view control
        /// </summary>
        public PeopleView()
        {
            peopleItemSource = new ObservableCollection<Person>();
            SelectedItems = new List<object>();

            InitializeComponent();
        }

        /// <summary>
        /// Reads data from database, binds result to the view
        /// </summary>
        public void UpdateViewFromItemSource()
        {
            PeopleItemSource = DbFacade.GetInstance().GetPeople();
            lvPeople.ItemsSource = PeopleItemSource;
        }

        /// <summary>
        /// Change selection event handler
        /// </summary>        
        private void lvPeople_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedItems = ((ListView)sender).SelectedItems;
        }

        /// <summary>
        /// Sort by columns
        /// </summary>        
        void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked =
                  e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;

            if (headerClicked != null)
            {
                if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
                {
                    if (headerClicked != lastHeaderClicked)
                    {
                        direction = ListSortDirection.Ascending;
                    }
                    else
                    {
                        if (lastDirection == ListSortDirection.Ascending)
                        {
                            direction = ListSortDirection.Descending;
                        }
                        else
                        {
                            direction = ListSortDirection.Ascending;
                        }
                    }

                    string header = (headerClicked.Column.Header as string).Replace(" ", String.Empty);
                    Sort(header, direction);

                    if (direction == ListSortDirection.Ascending)
                    {
                        headerClicked.Column.HeaderTemplate =
                          Resources["HeaderTemplateArrowUp"] as DataTemplate;
                    }
                    else
                    {
                        headerClicked.Column.HeaderTemplate =
                          Resources["HeaderTemplateArrowDown"] as DataTemplate;
                    }

                    // Remove arrow from previously sorted header
                    if (lastHeaderClicked != null && lastHeaderClicked != headerClicked)
                    {
                        lastHeaderClicked.Column.HeaderTemplate = null;
                    }


                    lastHeaderClicked = headerClicked;
                    lastDirection = direction;
                }
            }
        }

        /// <summary>
        /// Sorting implementaion for list view items
        /// </summary>
        /// <param name="sortBy">column name, should not contain spaces</param>
        /// <param name="direction">ListSortDirection enum value</param>
        private void Sort(string sortBy, ListSortDirection direction)
        {
            ICollectionView dataView =
              CollectionViewSource.GetDefaultView(lvPeople.ItemsSource);

            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        }
    }
}
